<template>
	<view class="container">
		<view class="tabs">
			<template v-for="(item, index) in tabList">
				<view v-key="index" class="tab" :class="(currentTab == index) ? 'selected' : ''" @click="toTab(index)">{{item}}</view>
			</template>
			
		</view>
		<template v-if="currentTab != 5">
			<view v-if="orderList.length > 0" class="order-box" style="background-color: #f1f1f1;">
				<view class="order-item" v-for="(item, index) in orderList" v-key="index">
					<view @click="enterDetail(item.id)">
						<view class="order-item-orderno">
							<view class="orderno">订单编号:{{item.order_no}}</view>
							<view class="status">{{item.order_status_txt}}</view>
						</view>
						<view class="order-item-main" v-for="(item1, index1) in item.orderitem" v-key="index1">
							<view class="order-item-main-left">
								<image :src="item1.image"></image>
							</view>
							<view class="order-item-main-right">
								<view class="title-box">
									<text class="title">{{item1.name}}</text>
									<text class="price">￥{{item1.price}}</text>
								</view>
								<!-- <view class="sku-box">
									<text>{{ item1.props_text }}</text>
								</view> -->
								<view class="title-box">
									<text></text>
									<text class="counts">x{{item1.num}}</text>
									<!-- <text class="return_status">{{item1.return_status_txt}}</text> -->
								</view>
							</view>
						</view>
						<view class="order-item-account">
							<view></view>
							<view>
								<text style="color: #707070;">共{{ item.total_num }}件商品 合计: </text>
								<text style="color: #C8A063;font-weight: bold;">¥{{item.total_price}}</text>
							</view>
						</view>
					</view>
					
					<view class="order-item-bottom" style="height: 40px;" v-if="item.order_status > 1 || item.order_status == 0">
						<view class="left">{{ item.createtime }}</view>
						<view class="right">
							<template v-if="item.order_status == 0">
								<view class="pay" @click="payOrder(item.id)">去付款</view>
								<view class="cancel" @click="cancel(item.id)">取消订单</view>
							</template>
								
							<template v-if="item.order_status == 2">
								<view class="pay" @click="queryOrder(item.id)">确认收货</view>
							</template>
							<template v-if="item.order_status == 3 && item.is_comment == 0">
								<view class="pay" @click="commentOrder(item.id)">去评价</view>
							</template>

							<!-- <template v-if="item.delivery_status != 1">
								<view class="look" @click="readExpress">查看物流</view>
							</template> -->
						</view>
					</view>
				</view>
			</view>
			<view v-else class="no-data" style="line-height: 29px;padding-top: 60%;">
				<image style="margin-bottom:-4px;height: 50px;width: 50px; margin-right: 8px;  line-height: 38px;"
					src="/static/zanwu.png"></image>暂无相关订单数据
			</view>
		</template>
		
		<template v-else>
		<view v-if="orderList.length > 0" class="order-box" >
			<view class="order-item" v-for="(item, index) in orderList" v-key="index" @click="enterDetail(item.id)">
				<view class="order-item-orderno">
					<view class="orderno">售后单号:{{item.out_refund_no}}</view>
					<view class="status">{{item.return_type_txt}}</view>
				</view>
				<view class="order-item-main">
					<view class="order-item-main-left">
						<image :src="item.ordergoods.image"></image>
					</view>
					<view class="order-item-main-right">
						<view class="title-box">
							<text class="title">{{item.ordergoods.title}}</text>
							<text class="price">￥{{item.ordergoods.sell_price}}</text>
						</view>
						<view class="sku-box">
							
						</view>
						<view class="title-box">
							<text class="counts">x{{item.ordergoods.num}}</text>
							<text class="return_status"></text>
						</view>
					</view>
				</view>
				<view class="order-item-account">
					<view v-if="item.return_status == 1 && item.ordergoods.return_status == 3">卖家已同意，请退货</view>
					<view v-else>{{item.return_status_txt}}</view>
					<view></view>
				</view>
				<view class="order-item-account">
					<view>{{item.create_time}}</view>
					<view><text>退款，退款金额: </text><text>¥{{item.return_price}}</text></view>
				</view>
			</view>
		</view>
		<view v-else class="no-data">暂无相关订单数据</view>
		</template>
		
		<!-- 取消订单 -->
		<view class="tui-actionsheet" :class="showActionSheet.show?'tui-actionsheet-show':'tui-actionsheet-none'">
			<view class="actionsheet-title">{{showActionSheet.title}}</view>
			<view v-for="(item, index) in showActionSheet.itemList" v-key="index" class="actionsheet-btn-item" @click="handleClickItem(index)">
				<text>{{item.text}}</text>
				<image v-if="item.show" src="/static/ok.png"></image>
			</view>
			<view class="tui-actionsheet-bottom">
				<view class="actionsheet-cancel" @click="handleClickCancel">取消</view>
				<view class="actionsheet-query" v-if="showActionSheet.showquery" @click="handleClickQuery">确定
				</view>
				<view class="actionsheet-query-no" v-if="!showActionSheet.showquery"></view>
			</view>
		</view>
		<view class="tui-actionsheet-mask" :class="showActionSheet.show?'tui-mask-show':'tui-mask-none'" @click="handleClickCancel"></view>
	</view>
</template>

<script>
	import { Order } from '@/utils/model/order-model.js';
	var order = new Order();
	
	import { Pay } from '@/utils/model/pay-model.js';
	var pay = new Pay();
	
	export default {
		data() {
			return {
				currentTab:0,
				//tabList:['全部', '待付款', '待发货', '待收货','待评价','售后'],
				tabList:['全部', '待付款', '待发货', '待收货','待评价'],
				orderList:[],
				orderReturnList: [],
				page: 1,
				pageReturn: 1,
				isLoadedAll:false,
			
				showActionSheet: {
					show: false,
					title:'请选择取消原因',
					itemList:[{
						text: '选择错误了',
						show: false
					},{
						text: "收货地址填写错误了",
						show: false
					},{
						text: "价格较贵",
						show: false
					},{
						text: "不想要了",
						show: false
					},{
						text: "其他",
						show: false
					}],
					showquery: false,
					id: 0,//选中取消订单id
				},
			}
		},
		onShow() {
		    this._onLoad(0)
		},
		
		methods: {
			toTab(index){
			    let type = 0;//0刷新  1增加
			    this.currentTab = index
			    this._onLoad(0)
			},
			enterDetail(id){
			    if(this.currentTab == 5){
			      uni.navigateTo({
			        url: '/pages/order/refunddetail?id=' + id
			      })
			    }else{
			      uni.navigateTo({
			        url: '/pages/order/detail?id=' + id
			      })
			    }
			    
			},
			
			//付款
			payOrder(id){
				uni.navigateTo({
					url: '/pages/order/pay?id=' + id
				})
			},
			  //---------------------------------取消订单 start
			  cancel(orderId){
			    this.showActionSheet.show = true;
			    this.showActionSheet.id = orderId;
			    this.showActionSheet = this.showActionSheet
			  },
			  handleClickCancel(){
			    this.showActionSheet.show = false;
			    this.showActionSheet = this.showActionSheet
			  },
			  handleClickItem(index){
			    if(!this.showActionSheet.show) return;
			    for(let i  = 0; i < this.showActionSheet.itemList.length; i++){
			      this.showActionSheet.itemList[i].show = false;
			    }
			    this.showActionSheet.itemList[index].show = true;
			    this.showActionSheet.showquery = true;
			    this.showActionSheet = this.showActionSheet
			  },
			  handleClickQuery(){
			    let that = this;
			    let reason = '';
			    let orderId = 0;//订单id
			    for(let i  = 0; i < this.showActionSheet.itemList.length; i++){
					if(this.showActionSheet.itemList[i].show === true){
						reason = this.showActionSheet.itemList[i].text;
						orderId = this.showActionSheet.id;
					}
			    }
			    
			    if(orderId === 0){
					uni.showModal({
						title: '友情提示',
						content: '请选择取消订单原因',
						showCancel: false,
						success() {
							uni.navigateBack()
						}
					}) 
			      return
			    }
			    
			    order.cancel(orderId, reason).then(res=>{
			      console.log(res)
			      uni.showToast({
			        title: res.message,
			        icon:'none'
			      })
			      that.handleClickCancel();
			      that._onLoad(0)
			    })
			  },
			  //---------------------------------取消订单 over
			  //确认收货
			  queryOrder(order_id){
			    let that = this;
			    uni.showModal({
			      title: '确认收货',
			      content: '是否确认收货',
			      success: (res) => {
			        if(res.confirm){
			          order.receipt(order_id).then(res=>{
			            uni.showToast({
			              title: res.msg,
			              icon: "none"
			            })
			            if(res.code == 1){
			              that._onLoad(0);
			            }
			          })
			        }else{
			          console.log('cancel')
			        }
			      }
			    })
			  },
			  commentOrder(orderId){
			    uni.navigateTo({
			      url: '/pages/order/comment?id='+ orderId
			    })
			  },
			  refundOrder(e){
			    let id = e.currentTarget.dataset.id;//订单id
			    uni.navigateTo({
			      url: 'refund?id='+id
			    })
			  },
			  readExpress(e){
			    let index = e.currentTarget.dataset.index;//订单id
			    let one = this.data.orderList[index];
			    let id = one.id;
			    let delivery_name = one.delivery_name;
			    let delivery_no = one.delivery_no;
			    uni.navigateTo({
			      url: 'express?id='+id + '&delivery_name='+delivery_name + '&delivery_no='+delivery_no
			    })
			  },
			  /**
			   * @param {Object} type 0:刷新， 1加载
			   */
			_onLoad(type){
			    let that = this;
			    let page = that.page;
			    if(type == 0){
			      page = 1;
			    }
				let status = that.currentTab - 1;//
				//4 售后
				if(status == 4){
					
				}else{
					order.getList(status, page).then(res => {
					    console.log(res);
					    console.log('3333333333333333333333333333333')
					    if(res.code == 1){
							let current_page = res.data.current_page
							let orderList = res.data.orderList
							if(type == 0) {//刷新
								that.page = current_page
								that.orderList = orderList,
								that.isLoadedAll = false //是否加载完全
								uni.stopPullDownRefresh()
							}else{
								if(orderList.length > 0){
									that.orderList.push.apply(that.orderList, orderList);  //数组合并
									that.page = current_page,
									that.orderList = orderList
								}	
							}
					    }
					})
				}
			},
			  onPullDownRefresh() {
			    this._onLoad(0)
			  },
			
			  onReachBottom() {
			    if(!this.isLoadedAll) {
			      this.page++;
			      this._onLoad(1)
			    }
			  },
		}
	}
</script>


<style>
	page{
		background-color: #F5F5F5;
	}
</style>
 
<style scoped lang="scss">
	@import '~@/pages/order/list.scss';
</style>
